﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;
using ZR.Service.Service.ProductionControl;

namespace CKS.MES.Server.Controllers.Business.ProductionControl {

    /// <summary>
    /// 产品类型配置控制器
    /// </summary>
    [Verify]
    [Route("business/cksproducttypeconfig")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksProductTypeConfigController : BaseController {

        /// <summary>
        /// 产品类型配置表
        /// </summary>
        private readonly ICksProductTypeConfigService _cksProductTypeConfigService;

        public CksProductTypeConfigController(ICksProductTypeConfigService cksProductTypeConfigService) {
            _cksProductTypeConfigService = cksProductTypeConfigService;
        }

        /// <summary>
        /// 查询产品类型配置表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "produce:cksproducttypeconfig:list")]
        public IActionResult QueryCksProductTypeConfig([FromQuery] CksProductTypeConfigQueryDto parm) {
            
            var response = _cksProductTypeConfigService
                .Queryable()
                .WhereIF(!string.IsNullOrEmpty(parm.TypeDesc),o=>o.TypeDesc == parm.TypeDesc)
                .ToPage(parm);

            return SUCCESS(response);
        }

        /// <summary>
        /// 查询产品类型配置表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "produce:cksproducttypeconfig:query")]
        public IActionResult GetCksProductTypeConfig(int Id) {
            var response = _cksProductTypeConfigService.Queryable()
              .Where(x => x.Id == Id)
              .First();
            return SUCCESS(response);
        }

        /// <summary>
        /// 添加产品类型配置表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "produce:cksproducttypeconfig:add")]
        [Log(Title = "产品类型配置表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksProductTypeConfig([FromBody] CksProductTypeConfigDto parm) {
            var modal = parm.Adapt<CksProductTypeConfig>().ToCreate(HttpContext);
            modal.Created = DateTime.Now;
            var response = _cksProductTypeConfigService.Insertable(modal).ExecuteCommand();
            return SUCCESS(response);
        }

        /// <summary>
        /// 更新产品类型配置表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "produce:cksproducttypeconfig:edit")]
        [Log(Title = "产品类型配置表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksProductTypeConfig([FromBody] CksProductTypeConfigDto parm) {
            var modal = parm.Adapt<CksProductTypeConfig>().ToUpdate(HttpContext);
            var response = _cksProductTypeConfigService.Update(modal, true);
            return ToResponse(response);
        }

        /// <summary>
        /// 删除产品类型配置表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "produce:cksproducttypeconfig:delete")]
        [Log(Title = "产品类型配置表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksProductTypeConfig([FromRoute] string ids) {
            var idArr = Tools.SplitAndConvert<int>(ids);
            return ToResponse(_cksProductTypeConfigService.Delete(idArr));
        }

    }

}
